const mongoose = require("mongoose");

const { Schema } = mongoose;

const schema = new Schema({
  name: String,
  parent: {
    type: Schema.Types.ObjectId,
    ref: "Category"
  }
}, {
  toJSON: { virtuals: true }
});

schema.virtual("children", {
  ref: "Category",
  localField: "_id",
  foreignField: "parent",
  justOne: false
})

schema.virtual("articles", {
  ref: "Article", // 引用的model
  localField: "_id",
  foreignField: "categories",
  justOne: false
})

schema.virtual("heroes", {
  ref: "Hero", // 引用的model
  localField: "_id",
  foreignField: "categories",
  justOne: false
})

module.exports = mongoose.model("Category", schema);
